package com.teamone.etl.accountant.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.accountant.dao.FinanceDao;
import com.teamone.etl.accountant.dao.InvoiceSpecificationDao;
import com.teamone.etl.accountant.dao.RechargeSheetDao;
import com.teamone.etl.accountant.dao.TicketAddressDao;
import com.teamone.etl.accountant.entity.Finance;
import com.teamone.etl.accountant.entity.InvoiceSpecification;
import com.teamone.etl.accountant.entity.RechargeSheet;
import com.teamone.etl.accountant.entity.TicketAddress;
import com.teamone.etl.accountant.feign.EnterpriseFeign;
import com.teamone.etl.accountant.service.FinanceService;
import com.teamone.etl.constant.EtlConstant;
import com.teamone.etl.enterprise.entity.Enterprise;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 财务管理(Finance)表服务实现类
 *
 * @author makejava
 * @since 2021-01-21 11:58:11
 */
@Service("financeService")
@Transactional(rollbackFor = Exception.class)
public class FinanceServiceImpl extends ServiceImpl<FinanceDao, Finance> implements FinanceService {

    /**
     * 循环参数  循环6次  月份只查找到半年
     */
    public static  final int SIX = 6;

    @Resource
    private FinanceDao financeDao;
    @Resource
    private RechargeSheetDao rechargeSheetDao;
    @Resource
    private InvoiceSpecificationDao invoiceSpecificationDao;
    @Resource
    private EnterpriseFeign enterpriseFeign;
    @Resource
    private TicketAddressDao ticketAddressDao;

    /**
     * 订单列表模糊+分页查询
     * @param rechargeId    订单编号
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @param page          当前页
     * @param size          每页条数
     * @return              部分数据
     */
    @Override
    public PageInfo<RechargeSheet> financeSearch(String rechargeId, String startTime, String endTime, Integer page, Integer size) {
        //分页实现
        PageHelper.startPage(page, size);
        //查询数据
        List<RechargeSheet> search = rechargeSheetDao.financeSearch(rechargeId, Timestamp.valueOf(startTime), Timestamp.valueOf(endTime));
        return new PageInfo<>(search);
    }

    /**
     *  待审核订单
     * @param page  当前页
     * @param size  每条数据
     * @return      每条数据
     */
    @Override
    public PageInfo<Finance> orderReview(Integer page, Integer size) {
        //分页实现
        PageHelper.startPage(page,size);
        //查询未审核的数据
        List<Finance> review = financeDao.orderReview();
        return new PageInfo<>(review);
    }

    /**
     *  通过订单/不通过订单
     * @param id    主键
     * @param flag  1:通过,2:不通过
     * @return      是否执行成功
     */
    @Override
    public Boolean reviewPass(Integer id, Integer flag) {
        //获取到当前主键的对象
        Finance finance = financeDao.selectById(id);
        finance.setIsReview(flag);
        int i = financeDao.update(finance);
        return i > 0;
    }

    /**
     * 财务统计
     * @return 每个月的总金额
     */
    @Override
    public Map<String, BigDecimal> accountStatics() {
        Map<String,BigDecimal> map = new HashMap<>(16);
        //获取当前月份和年份
        int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        int year = Calendar.getInstance().get(Calendar.YEAR);
        //循环六次 只获取前半年的财务统计
        for (int i = SIX; i > EtlConstant.Number.NONE; i--) {
            BigDecimal sum1 = new BigDecimal(0);
            //如果是个位月份  就+0
            if(month>0 && month <10){
                //根据月份查询到当前信息
                List<RechargeSheet> account = rechargeSheetDao.selectAccount(year+"-0"+month);
                //循环获取订单金额 相加
                for (RechargeSheet rechargeSheet : account) {
                    sum1 = rechargeSheet.getTradingMoney().add(sum1);
                }
            //十位月份 不+0
            }else{
                List<RechargeSheet> account = rechargeSheetDao.selectAccount(year+"-"+month);
                for (RechargeSheet rechargeSheet : account) {
                    sum1 = rechargeSheet.getTradingMoney().add(sum1);
                }
            }
            //数据使用map集合存储  键为年月 值为当月的总额
            map.put(year+"-"+month,sum1);
            month--;
            if(month == 0){
                month = 12;
                year = year-1;
            }
        }
        return map;
    }

    /**
     * 发票审核列表
     * @return 全部数据
     */
    @Override
    public List<InvoiceSpecification> auditInvoice() {
        return invoiceSpecificationDao.search();
    }

    /**
     * 发票详情
     * @param id 主键
     * @return   部分数据
     */
    @Override
    public Map<String, Object> invoiceDetails(Integer id) {
        Map<String, Object> map = new HashMap<>(16);
        //查询发票信息
        InvoiceSpecification invoice = invoiceSpecificationDao.queryById(id);
        //通过关联的企业ID查询企业信息
        Enterprise enterprise = enterpriseFeign.getById(invoice.getEnterpriseId());
        //通过关联的充值编号 查询地址信息
        TicketAddress ticketAddress = ticketAddressDao.queryByRechargeId(invoice.getRechargeId());
        //添加到集合中
        map.put("enterprise",enterprise);
        map.put("ticketAddress",ticketAddress);
        return map;
    }

    /**
     *   通过发票/不通过发票
     * @param id    主键
     * @param flag  1:通过,2:不通过
     * @return      是否执行成功
     */
    @Override
    public Boolean reviewInvoice(Integer id, Integer flag) {
        //获取到当前主键的对象
        InvoiceSpecification invoice = invoiceSpecificationDao.queryById(id);
        //改变当前发票的状态
        invoice.setInvoiceStatus(flag);
        //修改当前发票  如果改变影响行数>0 返回true
        return invoiceSpecificationDao.update(invoice) > 0;
    }
}